﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace NGM.MicrosoftConnect {
    public class MicrosoftConnectOAuthRequestBuilder {
        private readonly string _clientId;
        private readonly string _clientSecret;
        private readonly Uri _redirectUri;
        private readonly ResponseType _responseType;
        private readonly List<Scope> _scopes = new List<Scope>();

        public MicrosoftConnectOAuthRequestBuilder(string clientId, string clientSecret, Uri redirectUri, ResponseType responseType) {
            _clientId = clientId;
            _clientSecret = clientSecret;
            _redirectUri = redirectUri;
            _responseType = responseType;
        }

        public MicrosoftConnectOAuthRequestBuilder(string clientId, string clientSecret, Uri redirectUri, ResponseType responseType, IEnumerable<Scope> scopes)
            : this(clientId, clientSecret, redirectUri, responseType) {
            _scopes = scopes.ToList();
        }

        private Display _display = Display.Popup;
        private CultureInfo _locale = null;
        private GrantType _grantType = GrantType.Authorization_Code;
        private string _refreshToken = string.Empty;

        public MicrosoftConnectOAuthRequestBuilder WithRefreshToken(string refreshToken) {
            _refreshToken = refreshToken;
            return this;
        }

        public MicrosoftConnectOAuthRequestBuilder WithDisplay(Display display) {
            _display = display;
            return this;
        }

        public MicrosoftConnectOAuthRequestBuilder WithGrantType(GrantType grantType) {
            _grantType = grantType;
            return this;
        }

        public MicrosoftConnectOAuthRequestBuilder WithLocale(CultureInfo locale) {
            _locale = locale;
            return this;
        }

        public MicrosoftConnectOAuthRequestBuilder AddScope(Scope scope) {
            _scopes.Add(scope);
            return this;
        }

        public MicrosoftConnectOAuthRequest Build() {
            if (!MicrosoftConnectOAuthRequest.HasRequestCode())
                throw new NoOAuthRequestCodeException();

            var request = new MicrosoftConnectOAuthRequest();
            request.ClientId = _clientId;
            request.ClientSecret = _clientSecret;
            request.RedirectUri = _redirectUri;
            request.ResponseType = _responseType;
            request.Scopes = _scopes;

            if (!string.IsNullOrEmpty(_refreshToken) || _grantType == GrantType.Refresh_Token) {
                request.GrantType = GrantType.Refresh_Token;
                request.RefreshToken = _refreshToken;
            }
            else {
                request.GrantType = _grantType;
            }

            request.Display = _display;
            if (_locale != null)
                request.Locale = _locale;

            return request;
        }
    }
}